<?php
/**
 * Created by PhpStorm.
 * User: lelouchms
 * Date: 2019/3/28
 * Time: 15:55
 */

namespace app\backend\controller\admin\v1;


use app\backend\validate\common\LoginValidate;
use app\common\exception\ValidateException;

class Login extends Base
{

    protected $allow_action = ["index"];

    public function index()
    {
        $requestData = $this->request->post();
        $validate = new LoginValidate();
        $flag = $validate->check($requestData);
        if ($flag === false) {
            throw new ValidateException($validate->getError());
        }
        $userData = $this->loginByPassword($requestData["username"], $requestData["password"]);
        $this->saveSession($userData);
        return [
            "user_info" => [
                "id" => $userData["id"],
                "username" => $userData["username"],
                "nickname" => $userData["nickname"],
                "avatar" => $userData["avatar"],
            ],
            "token" => $userData["token"]
        ];
    }

}